﻿package {
	
	import components.Menu;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class NavigationMain extends Sprite 
	{
		private var navigation:Menu;
		
		public function NavigationMain(): void
		{
			// 네비게이션 만듬.
			navigation = Menu.create({direction:'horizontal', position:new Point(30,30), target:new Point(30,30), space:5}, true);
			// 첫번째 뎁스 메뉴리스트
			var firstMenuList:Vector.<Menu> = Vector.<Menu>([
						Menu.create({direction:'horizontal', position:new Point(00,20), target:new Point(00,30), space:5}),
						Menu.create({direction:'horizontal', position:new Point(10,20), target:new Point(10,30), space:5}),
						Menu.create({direction:'horizontal', position:new Point(20,20), target:new Point(20,30), space:5})
						]);
			// 두번째 뎁스 메뉴리스트
			var secoundMenuList:Vector.<Menu> =  Vector.<Menu>([
						Menu.create({direction:'horizontal', position:new Point(00,50), target:new Point(00,60), space:5}),
						Menu.create({direction:'horizontal', position:new Point(10,50), target:new Point(10,60), space:5}),
						Menu.create({direction:'horizontal', position:new Point(20,50), target:new Point(20,60), space:5}),
						Menu.create({direction:'horizontal', position:new Point(30,50), target:new Point(30,60), space:5}),
						Menu.create({direction:'horizontal', position:new Point(40,50), target:new Point(40,60), space:5}),
						Menu.create({direction:'horizontal', position:new Point(50,50), target:new Point(50,60), space:5}),
						Menu.create({direction:'horizontal', position:new Point(60,50), target:new Point(60,60), space:5}),
						Menu.create({direction:'horizontal', position:new Point(70,50), target:new Point(70,60), space:5}),
						Menu.create({direction:'horizontal', position:new Point(80,50), target:new Point(80,60), space:5}),
						]);
			
			addChild(navigation);
			
			navigation.add(createButton('Menu0'), firstMenuList[0]);
			navigation.add(createButton('Menu1'), firstMenuList[1]);
			navigation.add(createButton('Menu2'), firstMenuList[2]);
			
			// 첫번째 서브뎁스 메뉴에 버튼 추가.
			firstMenuList[0].add(createButton('Menu00'), secoundMenuList[0]);
			firstMenuList[0].add(createButton('Menu01'), secoundMenuList[1]);
			firstMenuList[0].add(createButton('Menu02'), secoundMenuList[2]);
			firstMenuList[1].add(createButton('Menu10'), secoundMenuList[3]);
			firstMenuList[1].add(createButton('Menu11'), secoundMenuList[4]);
			firstMenuList[1].add(createButton('Menu12'), secoundMenuList[5]);
			firstMenuList[2].add(createButton('Menu20'), secoundMenuList[6]);
			firstMenuList[2].add(createButton('Menu21'), secoundMenuList[7]);
			firstMenuList[2].add(createButton('Menu22'), secoundMenuList[8]);
			// 두번째 서브뎁스 메뉴에 버튼 추가.
			secoundMenuList[0].add(createButton('Menu000'));
			secoundMenuList[0].add(createButton('Menu001'));
			secoundMenuList[0].add(createButton('Menu002'));
			secoundMenuList[1].add(createButton('Menu010'));
			secoundMenuList[1].add(createButton('Menu011'));
			secoundMenuList[1].add(createButton('Menu012'));
			secoundMenuList[2].add(createButton('Menu020'));
			secoundMenuList[2].add(createButton('Menu021'));
			secoundMenuList[2].add(createButton('Menu022'));
			secoundMenuList[3].add(createButton('Menu100'));
			secoundMenuList[3].add(createButton('Menu101'));
			secoundMenuList[3].add(createButton('Menu102'));
			secoundMenuList[4].add(createButton('Menu110'));
			secoundMenuList[4].add(createButton('Menu111'));
			secoundMenuList[4].add(createButton('Menu112'));
			secoundMenuList[5].add(createButton('Menu120'));
			secoundMenuList[5].add(createButton('Menu121'));
			secoundMenuList[5].add(createButton('Menu122'));
			secoundMenuList[6].add(createButton('Menu200'));
			secoundMenuList[6].add(createButton('Menu201'));
			secoundMenuList[6].add(createButton('Menu202'));
			secoundMenuList[7].add(createButton('Menu210'));
			secoundMenuList[7].add(createButton('Menu211'));
			secoundMenuList[7].add(createButton('Menu212'));
			secoundMenuList[8].add(createButton('Menu220'));
			secoundMenuList[8].add(createButton('Menu221'));
			secoundMenuList[8].add(createButton('Menu222'));
			
			// 첫번째, 서브뎁스 메뉴 자식으로 등록
			navigation.addChild(firstMenuList[0]);
			navigation.addChild(firstMenuList[1]);
			navigation.addChild(firstMenuList[2]);
			// 두번째, 서브뎁스 메뉴 자식으로 등록
			navigation.addChild(secoundMenuList[0]);
			navigation.addChild(secoundMenuList[1]);
			navigation.addChild(secoundMenuList[2]);
			navigation.addChild(secoundMenuList[3]);
			navigation.addChild(secoundMenuList[4]);
			navigation.addChild(secoundMenuList[5]);
			navigation.addChild(secoundMenuList[6]);
			navigation.addChild(secoundMenuList[7]);
			navigation.addChild(secoundMenuList[8]);
			
			navigation.addEventListener(MouseEvent.ROLL_OVER, addCheckMouseEvent );
		}
		
		// 네비게이션 외각으로 나갔을때, 사라지게 하기 위한 마우스 리스너 등록
		private function addCheckMouseEvent(e:Event): void
		{
			stage.addEventListener(MouseEvent.MOUSE_MOVE,checkMouseOut);
		}
		// 네비게이션 외각으로 나갔을때, 모두다 닫히게 하는 리스너
		private function checkMouseOut(e:Event): void 
		{
			var moveableRect:Rectangle = navigation.getBounds(stage);
			if(!moveableRect.contains(stage.mouseX,stage.mouseY)){
				navigation.closeAllSub();
				stage.removeEventListener(MouseEvent.MOUSE_MOVE,checkMouseOut);
			} 
		}
		
		// 버튼 만들기
		private function createButton(text:String): InteractiveObject
		{
			var margin:int = 10;
			var field:TextField = new TextField;
			var button:Sprite = new Sprite;
			var matrix:Matrix = new Matrix(1,0,0,1,0,0);
			field.autoSize = 'left';
			field.defaultTextFormat = new TextFormat('dotum',12,0x555555,true);
			field.text = text;
			matrix.createGradientBox(field.width+margin,field.height+margin,Math.PI/2,0,5);
			button.buttonMode = true;
			button.mouseChildren = false;
			button.x = button.y = 0 ;
			button.graphics.beginGradientFill(GradientType.LINEAR, [0xcccccc, 0xcccccc],[0.2,1],[0,255], matrix);
			button.graphics.drawRoundRect(0,0,field.width+margin,field.height+margin,4,4);
			field.x = button.width/2-field.width/2;
			field.y = button.height/2-field.height/2;
			button.addChild(field);			
			return button;
		}
		
	}//c
}